
var deviceId="34:14:B5:BB:c3:3E";//连接的设备的ID
var qiehuan_su=0;
var i=0;
var serviceId=[];//各种服务的ID
var characteristicId=[];//
var shujubianhuan=[];
var lyzhuangtai=0
Page({
  data: {
   
    selectBar:1,
    lyzhuangtai:1,
   text: "当前的蓝牙状态: 蓝牙已成功连接",
   text1: "当前的蓝牙状态: 蓝牙未成功连接",
    marqueePace: 1,//滚动速度
    marqueeDistance: 0,//初始滚动距离
    marquee_margin: 30,
    size:14,
    interval: 20 ,
   zi_sudu:'',
   zi_moshi:'',

   logs: [],
   StartX:'0',//中点位置
   StartY:'40',//中点位置
   leftLooks: '', 
   topLooks: '',
   radius: '60',//半径
   angle: '0',
   widthview: 10,

   sendmsg:"",
   shujy:"31/.",
   long:"0",
   shumu:"",
   sousuo:"",
   status:"",
   zhuangtai:"未初始化",//
   shebei:"",//
   deviceName:"0",//
   deviceId:[],//
   devices:[],//
   connectedDeviceId:"34:14:B5:BB:c3:3E",//已连接设备UUid
   services:"",//连接设备的服务
   characteristics:"",//连接设备的状态值
   writeServicweId:"",//可写服务UUid
   writeCharacteristicsId:"",//可写特征值UUid
   readServicweId:"",//可读服务UUid
   readCharacteristicsId:"",//可读特征值UUid
   notifyServicweId:"",//通知服务UUid

  },
   /**
    * 生命周期函数--监听页面加载
    */

   clickType(param) {
     let name=this.data.name
     console.log("点击获取的数据",param.currentTarget.dataset.id)
     this.setData({
       selectBar:param.currentTarget.dataset.id,
     })
  if(param.currentTarget.dataset.id==2)
     {
       wx.navigateTo({
         url: '../bo_shuoming/bo_shuoming'
       })
     }
 if(param.currentTarget.dataset.id==1)
     {
       wx.reLaunch({
         url: '../xiaoche/xiaoche'
       })
     }
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   // this.connectTO();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this;
      var length = that.data.text.length * that.data.size;//文字长度
      var windowWidth = wx.getSystemInfoSync().windowWidth;// 屏幕宽度
      that.setData({
       length: length,
       windowWidth: windowWidth,
       StartX:windowWidth/2,
      });
      that.scrolltxt();// 第一个字消失后立即从右边出现
  },
  scrolltxt: function () {
    var that = this;
    var length = that.data.length;//滚动文字的宽度
    var windowWidth = that.data.windowWidth;//屏幕宽度
    if (length<=windowWidth){
      that.setData({
         marquee_margin:windowWidth-length
      })
     var interval = setInterval(function () {
     var maxscrollwidth = length + that.data.marquee_margin;//滚动的最大宽度，文字宽度+间距，如果需要一行文字滚完后再显示第二行可以修改marquee_margin值等于windowWidth即可
     var crentleft = that.data.marqueeDistance;//初始滚动距离
     if (crentleft <= maxscrollwidth) {//当滚动的字体小于屏幕宽度时应用小于号
      that.setData({
      marqueeDistance: crentleft + that.data.marqueePace//加滚动速度
      })
     }
     else {
      that.setData({
      marqueeDistance: 0 // 直接重新滚动
      });
      clearInterval(interval);
      that.scrolltxt();
     }
     }, that.data.interval);
    }
    else{
     that.setData({ marquee_margin:windowWidth});//只显示一条不滚动右边间距加大，防止重复显示
    } 
    },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  qiehuan:function(){
    var that = this
    if(qiehuan_su==0)
    {      
      that.setData({
      sendmsg:0-(-48)+'4746'
    })
      qiehuan_su=9;
    }else{ qiehuan_su=0;
      that.setData({
        sendmsg:9-(-48)+'4746'
      })
    }
    that.sendData();
  },
  
  sendData:function(){
    var that = this
    var hex = that.data.sendmsg  //要发送的信息
    var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
      return parseInt(h,0)
    }))
     var buffer1 = typedArray.buffer
    wx.writeBLECharacteristicValue({
      deviceId: "34:14:B5:BB:C3:3E",
      serviceId: "0000ffe0-0000-1000-8000-00805f9b34fb",
      characteristicId: "0000ffe1-0000-1000-8000-00805f9b34fb",
      value: buffer1,
      // 这里的value是ArrayBuffer类型
      success: function (res) {
        that.setData({
          zhuangtai:"发送数据成功"
        })
      },
      fail(res){
        that.setData({
          zhuangtai:"发送数据失败"
        })
      }
    })
  },
  //摇杆点击事件
  ImageTouch: function (e) {
    console.log("点击")
  },
  //拖动摇杆移动
  ImageTouchMove: function (e) {
    var self = this;
    //console.log(e.touches[0].clientX)
    //e.touches[0].clientX是触碰的位置，需要减40使得图片中心跟随触碰位置
    var touchX = e.touches[0].clientX - 40;
    var touchY = e.touches[0].clientY - this.data.StartX*2;
    var movePos = self.GetPosition(touchX, touchY);
    //console.log("接触坐标：(" + touchX + "," + touchY + ")");
    // console.log("图片坐标：("+movePos.posX + "," + movePos.posY+")");
    self.setData({
      leftLooks: movePos.posX,
      topLooks: movePos.posY
    })
  },

  //松开摇杆复原
  ImageReturn: function (e) {
    var self = this;
    self.setData({
      leftLooks: self.data.StartX,
      topLooks: self.data.StartY,
      angle: "",
      sendmsg:7-(-48)+'4746'
    })
    self.sendData();
  },

  //获得触碰位置并且进行数据处理获得触碰位置与拖动范围的交点位置
  GetPosition: function (touchX, touchY) {
    var self = this;
    var DValue_X;
    var Dvalue_Y;
    var Dvalue_Z;
    var imageX;
    var imageY;
    var ratio;
    DValue_X = touchX - self.data.StartX;
    Dvalue_Y = touchY - self.data.StartY;
    Dvalue_Z = Math.sqrt(DValue_X * DValue_X + Dvalue_Y * Dvalue_Y);
    self.GetAngle(DValue_X, Dvalue_Y);
    //触碰点在范围内
    if (Dvalue_Z <= self.data.radius) {
      imageX = touchX;
      imageY = touchY;
      imageX = Math.round(imageX);
      imageY = Math.round(imageY);
      return { posX: imageX, posY: imageY };
    }
    //触碰点在范围外
    else {
      ratio = self.data.radius / Dvalue_Z;
      imageX = DValue_X * ratio + this.data.StartX;
      imageY = Dvalue_Y * ratio + 40;
      imageX = Math.round(imageX);
      imageY = Math.round(imageY);
      return { posX: imageX, posY: imageY };

    }
  },
  //获取角度
  GetAngle: function (DValue_Y, Dvalue_X) {
    // console.log(DValue_Y)
    var self = this;
    if (DValue_Y != 0) {
      var angleTan = Dvalue_X / DValue_Y;
      var ang = Math.atan(angleTan);
      var angs = ang * 180 / 3.14;
      var result = Math.round(angs);
      if((self.data.leftLooks>self.data.StartX)&&(self.data.topLooks<self.data.StartY)){result=-result}
      if(self.data.leftLooks<self.data.StartX){result=180-result}
      if((self.data.leftLooks>self.data.StartX)&&(self.data.topLooks>self.data.StartY)){result=360-result}
      if((self.data.leftLooks==self.data.StartX)&&(self.data.topLooks==(self.data.StartY-self.data.radius)))
      {  result=90}
      if(((self.data.leftLooks==(self.data.StartX-1))||(self.data.leftLooks==(self.data.StartX))||(self.data.leftLooks-1==(self.data.StartX))
      )&&(self.data.topLooks==((self.data.StartY)-(-self.data.radius))))
      { result=270}
      if(result>=0&&result<=70){
        self.setData({
          sendmsg:1-(-48)+'4746'
        })
      }
      if(result>70&&result<=110){
        self.setData({
          sendmsg:2-(-48)+'4746'
        })
      }
      if(result>110&&result<=180){
        self.setData({
          sendmsg:3-(-48)+'4746'
        })
      }
     if(result>180&&result<=250){
          self.setData({
            sendmsg:4-(-48)+'4746'
          })
      }
      if(result>250&&result<=290){
        self.setData({
          sendmsg:5-(-48)+'4746'
        })
    }
  if(result>290&&result<=360){
    self.setData({
      sendmsg:6-(-48)+'4746'
    })
}
      self.setData({
        angle: result
      })
    }
self.sendData();
  },
})
